package com.zstax.demo.encrypt;

import com.alibaba.fastjson.JSON;
import com.zstax.demo.encrypt.util.sm3.SM3Util;
import com.zstax.demo.encrypt.util.sm4.SM4Util;

import java.util.Map;

/**
 * 数据加密、签名生成示例
 */
public class EncryptDemo {
    public static void main(String[] args) {
        String appId = "sqzl202305080001";
        String appKey = "eeccb8ba67154040a5dfa4f1a8a013a5";
        long time = 1679037289;
        String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsicmVzb3VyY2VfaWQiXSwic2NvcGUiOlsiYWxsIiwic2VsZWN0Il0sImV4cCI6MTk5NjEyMTgyOSwiYXV0aG9yaXRpZXMiOlsiYXV0Il0sImp0aSI6IjRlOGNmYjEyLTk3YWEtNGQ3My05NjMzLTFiNWQxNTBmZGE3MSIsImNsaWVudF9pZCI6InNxemwyMDIyMDUzMTAwMDEifQ.xWvSB7aN4i4oZi1kgShZNCf-i4k9b72rExlpaSronbo";
        String requestId = "f8b32376363d415c8a950549b6f20841";//UUID.randomUUID().toString().replace("-","");
        String data = "{\n" +
                "    \"nsrsbh\": \"91110108MA00BBMB77\",\n" +
                "    \"nsrmc\": \"北京中税双运财税科技有限公司\",\n" +
                "    \"taxData\": {\n" +
                "        \"nsrsbh\": \"91110108MA00BBMB77\",\n" +
                "        \"skssq\": 202201,\n" +
                "        \"fplx\": \"01\",\n" +
                "        \"page\": {\n" +
                "            \"pageSize\": 1,\n" +
                "            \"currentPage\": 1\n" +
                "        }" +
                "    }\n" +
                "}";
        Map baseRequest = JSON.parseObject(data, Map.class);
        System.out.println(baseRequest);
        //加密data
        data = encryptData(baseRequest,appKey);
        ClientRequest clientRequest = new ClientRequest();
        clientRequest.setRequestId(requestId);
        clientRequest.setAppId(appId);
        clientRequest.setTime(time);
        clientRequest.setToken(token);
        clientRequest.setData(data);
        //签名
        String sign = buildSign(clientRequest,appKey);

        System.out.println("请求头：");
        System.out.println("appId:"+appId);
        System.out.println("time:"+time);
        System.out.println("requestId:"+requestId);
        System.out.println("token:"+token);
        System.out.println("sign:"+sign);
        System.out.println("\n请求体：");
        System.out.println("{\"data\":\"" + data + "\"}");
    }

    /**
     * 加密数据
     * @param data 数据
     * @return 密文
     */
    public static String encryptData(Object data,String secretKey) {
        //转为json字符串
        String dataStr = JSON.toJSONString(data);
        String encryptData = null;
        try {
            encryptData = SM4Util.encryptData(dataStr, secretKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptData;
    }

    /**
     * 构建签名
     * @param req 请求参数
     * @return 签名
     */
    public static String buildSign(ClientRequest req, String appKey) {
        String sb = appKey + req.getAppId() + req.getData() + req.getRequestId() +
                req.getTime() + req.getToken();
        return SM3Util.encrypt(sb);
    }
}
